/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entidades.controladores;

import entidades.ComponentesPc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 *
 * @author alphyon
 */
public class ComponentesPcJDBC {
    PreparedStatement ps = null;
    Connection conn = null;
    ResultSet rs = null;


    public ComponentesPcJDBC(){
        cargarDriver();
        coneccion();

    }

     public void cargarDriver(){
       try{
           Class.forName("org.gjt.mm.mysql.Driver");
           System.out.println("Carga de driver Correcta......");
       }catch(Exception e){
           System.out.print("A ocurrido Un problema" +
                   "en la caraga del Driver"+e.getMessage());
       }
   }

   public void coneccion(){

       try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventario_control", "chepe", "gundam");
            System.out.println("Conexion Satisfactoria...");
        } catch(SQLException e) {
            e.printStackTrace();
        }

   }


    public int crearComponentesPc(ComponentesPc comp) {
        int flag=0;

        try{
        String SQL="";
        SQL=SQL+"INSERT INTO componentes_pc (id,id_pc,id_componente)";
        SQL=SQL+" VALUES(?,?,?);";

        ps = conn.prepareStatement(SQL);

        ps.setInt(1, comp.getId());
        ps.setString(2, comp.getIdPc());
        ps.setString(3, comp.getIdComponente());
        


        flag=ps.executeUpdate();

        }catch(Exception e){
           e.printStackTrace();
        }finally{
             try {
              if (ps != null) ps.close();
              if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

       return flag;
    }

    public int borrarRegistroComponentesPc(int id){
        int flag=0;
        String SQL = "DELETE FROM componentes_pc WHERE id=?";


        try {
            ps = conn.prepareStatement(SQL);
            ps.setInt(1, id);
            flag=ps.executeUpdate();
        } catch (SQLException e) {
           e.printStackTrace();
        }

        return flag;
    }
    public int actualizarComponentesPc(ComponentesPc comp) {
        int flag=0;

        try{
        String SQL="";
        SQL=SQL+"UPDATE componentes_pc  SET id_pc=?,id_componente=? WHERE id=?";


        ps = conn.prepareStatement(SQL);


        ps.setString(1, comp.getIdPc());
        ps.setString(2, comp.getIdComponente());
        ps.setInt(3, comp.getId());

        flag=ps.executeUpdate();

        }catch(Exception e){
           e.printStackTrace();
        }finally{
             try {
              if (ps != null) ps.close();
              if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

       return flag;
    }
    public LinkedList<ComponentesPc> getComponentesPc(){
       LinkedList<ComponentesPc> lista=new LinkedList<ComponentesPc>();

       String SQL="SELECT * FROM datos_comp";
        try {
            ps = conn.prepareStatement(SQL);
            rs = ps.executeQuery();

            while(rs.next()){

                ComponentesPc comp = new ComponentesPc();
                comp.setId(rs.getInt("id"));
                comp.setIdComponente(rs.getString("id_componente"));
                comp.setIdPc(rs.getString("id_pc"));
                





                lista.add(comp);

            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
             try {
              if (rs != null) rs.close();
              if (ps != null) ps.close();
              if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return lista;


   }
    public static void  main(String[]args){
        ComponentesPc un = new ComponentesPc();
         ComponentesPcJDBC juni = new ComponentesPcJDBC();

         un.setId(0);
         un.setIdComponente("fjsdgdsf");
         un.setIdPc("pc11");
         System.out.print(juni.crearComponentesPc(un));


   }


}
